package cn.wolfcode.business.service.impl;

import java.util.Date;
import java.util.List;

import cn.wolfcode.business.domain.Appointment;
import cn.wolfcode.business.mapper.AppointmentMapper;
import cn.wolfcode.business.mapper.StatementItemMapper;
import cn.wolfcode.business.util.RegexUtils;
import cn.wolfcode.business.util.VehiclePlateNoUtil;
import cn.wolfcode.common.core.domain.entity.SysUser;
import cn.wolfcode.common.exception.ServiceException;
import cn.wolfcode.common.utils.DateUtils;
import cn.wolfcode.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wolfcode.business.mapper.StatementMapper;
import cn.wolfcode.business.domain.Statement;
import cn.wolfcode.business.service.IStatementService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 结算单Service业务层处理
 * 
 * @author dafei
 * @date 2023-10-31
 */
@Service
@Transactional
public class StatementServiceImpl implements IStatementService 
{
    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;

    /**
     * 查询结算单
     * 
     * @param id 结算单主键
     * @return 结算单
     */
    @Override
    public Statement selectStatementById(Long id)
    {
        return statementMapper.selectStatementById(id);
    }

    /**
     * 查询结算单列表
     * 
     * @param statement 结算单
     * @return 结算单
     */
    @Override
    public List<Statement> selectStatementList(Statement statement)
    {
        return statementMapper.selectStatementList(statement);
    }

    /**
     * 新增结算单
     * 
     * @param statement 结算单
     * @return 结果
     */
    @Override
    public int insertStatement(Statement statement)
    {
        //参数判断
        if(statement == null){
            throw new ServiceException("参数异常");
        }
        //做手机号码校验
        boolean phoneLegal = RegexUtils.isPhoneLegal(statement.getCustomerPhone());
        if(!phoneLegal){
            throw new RuntimeException("手机号码不正确");
        }
        //车牌号码校验
        VehiclePlateNoUtil.VehiclePlateNoEnum vehiclePlateNo = VehiclePlateNoUtil.getVehiclePlateNo(statement.getLicensePlate());
        if(vehiclePlateNo == null){
            throw new RuntimeException("车牌号不正确");
        }
        //做日期校验
//        boolean flag = statement.getActualArrivalTime().before(new Date());
//        if(flag){
//            throw new RuntimeException("日期时间不对");
//        }

        //设置状态--status
        statement.setStatus(Statement.STATUS_CONSUME);

        //创建时间
        statement.setCreateTime(DateUtils.getNowDate());
        return statementMapper.insertStatement(statement);
    }

    /**
     * 修改结算单
     * 
     * @param statement 结算单
     * @return 结果
     */
    @Override
    public int updateStatement(Statement statement)
    {
        //参数判断
        if(statement == null){
            throw new ServiceException("参数异常");
        }
        //设置状态---: 必须是消费中才允许改动
        Statement temp = statementMapper.selectStatementById(statement.getId());
        if(!Statement.STATUS_CONSUME.equals(temp.getStatus())){
            throw new ServiceException("必须是消费中状态才允许改动");
        }

        //做手机号码校验
        boolean phoneLegal = RegexUtils.isPhoneLegal(statement.getCustomerPhone());
        if(!phoneLegal){
            throw new RuntimeException("手机号码不正确");
        }
        //车牌号码校验
        VehiclePlateNoUtil.VehiclePlateNoEnum vehiclePlateNo = VehiclePlateNoUtil.getVehiclePlateNo(statement.getLicensePlate());
        if(vehiclePlateNo == null){
            throw new RuntimeException("车牌号不正确");
        }
        return statementMapper.updateStatement(statement);
    }

    /**
     * 批量删除结算单
     * 
     * @param ids 需要删除的结算单主键
     * @return 结果
     */
    @Override
    public int deleteStatementByIds(Long[] ids)
    {
        //此处使用假删除/逻辑删除/软删除
        for (Long id : ids) {
            //必须是消费中才允许删除
            Statement statement = statementMapper.selectStatementById(id);
            if(statement == null || !Statement.STATUS_CONSUME.equals(statement.getStatus())){
                throw  new ServiceException("参数异常或状态异常");
            }
            //结算单:  将is_delete 改为已删除
            statement.setIsDelete(Statement.IS_DELETE_YES);
            statementMapper.updateStatement(statement);

            //结算单明细:  直接删除
            statementItemMapper.deleteByStatementId(id);

            //预约单: 改为到店状态
            if(statement.getAppointmentId() != null){
                appointmentMapper.updateStatus(statement.getAppointmentId(), Appointment.STATUS_ARRIVAL);
            }
        }
        return 1;
    }

    /**
     * 删除结算单信息
     * 
     * @param id 结算单主键
     * @return 结果
     */
    @Override
    public int deleteStatementById(Long id)
    {
        return statementMapper.deleteStatementById(id);
    }

    @Override
    public void pay(Long id) {

        //参数校验--id       //状态校验-
        Statement statement = statementMapper.selectStatementById(id);
        if(statement == null || !Statement.STATUS_CONSUME.equals(statement.getStatus())){
            throw  new ServiceException("参数异常或者状态异常");
        }

        //更新结算单---状态 + 付出时间 + 收款人--系统当前登录用户
        statement.setStatus(Statement.STATUS_PAID);
        statement.setPayTime(new Date());
        statement.setPayeeId(SecurityUtils.getUserId());

        statementMapper.updateStatement(statement);


        //@Todo  后续如果为预约单的结算--修改预约到状态为 已支付
        //判断当前结算是否绑定预约单
        if(statement.getAppointmentId() != null){
            appointmentMapper.updateStatus(statement.getAppointmentId(), Appointment.STATUS_PAYED);
        }

    }
}
